package com.atguigu.web;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.CardItem;
import com.atguigu.pojo.Cart;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.Utils;
import com.google.gson.Gson;
import com.mysql.jdbc.Util;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

/**
 * Servlet implementation class CartServlet
 */
public class CartServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private BookService bookService = new BookServiceImpl();
	
	protected void clear(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart != null) {
			cart.clear();
			response.sendRedirect(request.getHeader("referer"));
		}
	}
	
	protected void addItems(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int id = Utils.parseInt(request.getParameter("id"), 0);
		Book book = bookService.queryOne(id);
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart == null) {
			cart = new Cart();
			request.getSession().setAttribute("cart", cart);
		}
		/*if(book.getStock() <= 0) {
			return;
		}else {*/
			
			cart.addItem(new CardItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice()));
			
			request.getSession().setAttribute("last_product_name", book.getName());
			response.sendRedirect(request.getHeader("referer"));
		//}
		
	}
	
	protected void ajaxAddItems(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int id = Utils.parseInt(request.getParameter("id"), 0);
		Book book = bookService.queryOne(id);
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart == null) {
			cart = new Cart();
			request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
		}
		cart.addItem(new CardItem(0, book.getName(), 1, book.getPrice(), book.getPrice()));
		
		request.getSession().setAttribute("last_product_name", book.getName());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("last_product_name", book.getName());
		map.put("totalCount", cart.getTotalCount());
		Gson gson = new Gson();
		String json = gson.toJson(map);
		response.getWriter().write(json);
		
	}
	
	protected void deleteItems(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int id = Utils.parseInt(request.getParameter("id"), 0);
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		cart.deleteItems(id);
		//重定向到原来界面
		response.sendRedirect(request.getHeader("referer"));
	}
	
	protected void updateItems(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int id = Utils.parseInt(request.getParameter("id"), 0);
		int count = Utils.parseInt(request.getParameter("count"), 1);
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart != null) {
			cart.updateCount(id, count);
			response.sendRedirect(request.getHeader("referer"));
		}
	}

	protected void ajaxUpdateItems(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int itemId = Utils.parseInt(request.getParameter("id"), 0);
		int count = Utils.parseInt(request.getParameter("count"), 1);
		Cart cart = (Cart) request.getSession().getAttribute("cart");
		if(cart != null) {
			//修改每样商品数量的总价格
			BigDecimal updateCountPrice = cart.updateCount(itemId, count);
			
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("updateCountPrice", updateCountPrice);
			map.put("totalCount", cart.getTotalCount());
			map.put("totalPrice", cart.getTotalPrice());
			Gson gson = new Gson();
			String json = gson.toJson(map);
			response.getWriter().write(json);
		}
	}
}
